﻿namespace EFS.RC.CommunicatorContext.Operators
{
	public partial class HuntGroupInvite : EFS.RC.CommunicatorContext.Common.Pages.Controls.Control
	{
		#region Locals

		// Last time we send an invite
		System.DateTime? lastInviteTime = null;


		#endregion

		#region Delegates
		/// SelectionChanged
		public delegate void SelectionChangedDelegate(EFS.RC.CC.Messages.Communicator.Lync.Context.HuntGroupInvite inviteMessage);
		public SelectionChangedDelegate SelectionChanged;
		#endregion

		public HuntGroupInvite()
		{
			InitializeComponent();
			imageR.Visibility = System.Windows.Visibility.Collapsed;
			imageG.Visibility = System.Windows.Visibility.Collapsed;
			imageY.Visibility = System.Windows.Visibility.Collapsed;

			// ignore in design tool
			if (!System.ComponentModel.DesignerProperties.IsInDesignTool)
			{
				// load groups	
				UtilitiesService.HuntGroups_GetAllCompleted += new System.EventHandler<ServiceReference.HuntGroups_GetAllCompletedEventArgs>(UtilitiesService_HuntGroups_GetAllCompleted);
				LoadHuntGroups();
			}
		}


		/// Load conversation info
		public void LoadConversationInfo(
			EFS.RC.CC.Messages.Communicator.Lync.Conversation.Info convInfo)
		{
			// invite less then 3 seconds
			if (null == lastInviteTime)
			{
				imageG.Visibility = System.Windows.Visibility.Collapsed;
				imageR.Visibility = System.Windows.Visibility.Collapsed;
				imageY.Visibility = System.Windows.Visibility.Collapsed;
			}
			else
			{
				// get selected
				EFS.RC.CC.Messages.Communicator.Lync.Context.HuntGroupInvite huntGroupInvite = comboBoxHuntGroups.SelectedItem as EFS.RC.CC.Messages.Communicator.Lync.Context.HuntGroupInvite;

				// check
				if (null != huntGroupInvite && huntGroupInvite.Sip == convInfo.HuntGroupSip)
				{
					imageG.Visibility = System.Windows.Visibility.Visible;
					imageR.Visibility = System.Windows.Visibility.Collapsed;
					imageY.Visibility = System.Windows.Visibility.Collapsed;
				}
				else
				{
					if (lastInviteTime.Value.AddSeconds(3) > System.DateTime.Now)
					{
						imageG.Visibility = System.Windows.Visibility.Collapsed;
						imageR.Visibility = System.Windows.Visibility.Collapsed;
						imageY.Visibility = System.Windows.Visibility.Visible;
					}
					else
					{
						imageG.Visibility = System.Windows.Visibility.Collapsed;
						imageR.Visibility = System.Windows.Visibility.Visible;
						imageY.Visibility = System.Windows.Visibility.Collapsed;
					}
				}
			}
		}

		#region Events
		/// Loaded
		private void Control_Loaded(object sender, System.Windows.RoutedEventArgs e)
		{
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void UtilitiesService_HuntGroups_GetAllCompleted(object sender, ServiceReference.HuntGroups_GetAllCompletedEventArgs e)
		{
			// show wait DLG
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.Completed(this);

			// check
			if (null != e.Error)
			{
				ShowErrorMessage("Failed to retrieve huntgroups.", e.Error);

				// done
				return;
			}

			// check
			if (null == e.Result)
			{
				// done
				return;
			}

			try
			{
				// bind/fill form
				EFS.RC.CC.CH.Entities.HuntGroup[] huntgroups = EFS.Common.Serialization.Functions.Deserialize<EFS.RC.CC.CH.Entities.HuntGroup[]>(e.Result.ToString());

				// load to combo
				comboBoxHuntGroups.Items.Add(new EFS.RC.CC.Messages.Communicator.Lync.Context.HuntGroupInvite() { Name = " - ", Sip = "" });
				for (int k = 0; k < huntgroups.Length; k++)
				{
					comboBoxHuntGroups.Items.Add(new EFS.RC.CC.Messages.Communicator.Lync.Context.HuntGroupInvite() { Name = huntgroups[k].Name, Sip = huntgroups[k].Sip });
				}
			}
			catch (System.Exception ex)
			{
				ShowErrorMessage("Failed to retrieve data.", ex);
			}
		}

		// load groups async
		private void LoadHuntGroups()
		{
			// show wait DLG
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.InProgress(this, "Retrieving Hunt Groups");

			//call ws
			UtilitiesService.HuntGroups_GetAllAsync();
		}

		/// Selection changed
		private void comboBoxHuntGroups_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
		{
			// notify all registered
			if (null != this.SelectionChanged)
			{
				EFS.RC.CC.Messages.Communicator.Lync.Context.HuntGroupInvite huntGroupInvite = comboBoxHuntGroups.SelectedItem as EFS.RC.CC.Messages.Communicator.Lync.Context.HuntGroupInvite;
				if (null != huntGroupInvite)
				{
					// notify
					this.SelectionChanged(huntGroupInvite);

					// update last invite time
					if (EFS.Common.Verify.String(huntGroupInvite.Sip))
					{
						lastInviteTime = System.DateTime.Now;
					}
					else
					{
						lastInviteTime = null;
					}
				}
			}
		}

		#endregion
	}

}